#!/bin/sh
#
# -
# Copyright (c) 2008-2013 The OpenLISP Project
#                         Luigi Iannone <ggx@openlisp.org>
# 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# 
# $Header: http://openlisp.googlecode.com/svn/trunk/openlisp-core/unpatch-kernel.sh 17 2013-05-03 13:56:10Z ggx@openlisp.org $
#
# Remove OpenLISP patches from OS Kernel
#
# Contributors:
#              Luigi Iannone <ggx@openlisp.org>
#
#

#-------------------------------------------------------------------------
# Define Section
#-------------------------------------------------------------------------

setvar PATCHDIR "Kernel_Patches"

setvar OK " ------------------------------------------------> [ OK ]"
setvar KO " ------------------------------------------------> [FAIL]"


#-------------------------------------------------------------------------
# SubRoutines Section
#-------------------------------------------------------------------------


#-------------------------------------------------------------------------
# START: CheckRoot()
CheckRoot()
{
    echo
    echo "--------------------------------------------------------"
    echo "     OpenLISP Core - Kernel Patching Script"
    echo "--------------------------------------------------------"
    echo

    if [ $(whoami) != "root" ]
        then
            echo
            echo " ===> You must be root to proceed!"
            echo
            exit 1
    fi
}
# END: CheckRoot()
#-------------------------------------------------------------------------




#-------------------------------------------------------------------------
# START: FindOSVersion()
FindOSVersion()
{

    echo
    echo "--------------------------------------------------------"
    echo "     Finding Correct OS and Version"
    echo "--------------------------------------------------------"
    echo

    setvar OS $(uname -s)
    setvar VERSION $(uname -r)

    if [ -d "$PATCHDIR/$OS" ]
    then
        echo
        echo " ===> $OS Supported"
    else
        echo " ===> $OS"
        echo " Operating System non recognized (or not supported)!"
        echo
        exit 1
    fi

    if [ -d "$PATCHDIR/$OS/$VERSION" ]
    then
        echo
        echo " ===> $VERSION Supported"
        echo
    else
        echo " ===> $VERSION"
        echo " $OS Version non recognized (or not supported)!"
        echo
        exit 1
    fi

}
# END: FindOSVersion()
#-------------------------------------------------------------------------

#-------------------------------------------------------------------------
# START: ApplyPatch()
#
# Code Assumptions:
#   - all patches are in the tree $PATCHTREE/$OS/$VERSION
#     recursively explored
#   - patch files have extention .patch
#   - directory $PATCHTREE/$OS/$VERSION is equivalent to the root /.
#     For example files $PATCHTREEE/$OS/$VERSION/usr/src/sys/Makefile.patch
#     indicates a patch for th Makefile in the directory /usr/src/sys/ of
#     the corresponing OS (for supported versions).
#   - The Routine loks for patch files, then if existing copies back
#     the .orig file.
#   - NOTE the file >orig IS NOT deleted.
#
RemovePatch()
{

    echo
    echo "--------------------------------------------------------"
    echo "     Patching Files..."
    echo "--------------------------------------------------------"
    echo

    PATCHTREE=`ls -R "$PATCHDIR/$OS/$VERSION/" | grep ":$" | sed -e 's/:$//'`

    for I in $PATCHTREE
    do

        PATCHFILES=`ls $I/*.patch 2>/dev/null`

        if [ "$?" -eq 0 ]
        then

            for J in $PATCHFILES
            do

                PFILE=`echo $J | sed "s/$PATCHDIR/$OS/$VERSION//" | sed "s/.patch//"`

                echo  " ===> UN-Patching: $PFILE "

                if [ -f $PFILE.orig ]
                then
                    cp $PFILE.orig $PFILE
                    echo $OK
                else
                    echo $KO
                    echo " Original file <$PFILE.orig> does not exist"
                    echo -n " Skip this step and continue (not safe) [y/N]? "
                    stty cbreak         # or  stty raw
                    ANSWER=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
                    stty -cbreak
                    echo

                    case $ANSWER in  y*|Y*)
                        setvar OK 1
                        ;;
                    esac

                    if [ "$OK" != "1" ]
                    then
                        echo
                        echo " (!) OpenLISP Installation Failed"
                        exit 1
                    fi

                fi

            done

        fi

    done

}
# END: RemovePatch()
#-------------------------------------------------------------------------



#-------------------------------------------------------------------------
# MAIN

CheckRoot

FindOSVersion

RemovePatch

echo
echo "--------------------------------------------------------"
echo "    $OS Kernel Version $VERSION successfully UN-patched!"
echo "--------------------------------------------------------"
echo

exit 0

#-------------------------------------------------------------------------







 
